<template>
  <div class="school">
    <div class="school-title">
      <div class="school-title-left">学校中心</div>
      <div class="school-title-right">
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd">添加学校</el-button>
        <el-button  icon="el-icon-menu" @click="handleSearch">{{searchId ? '收起':'展开'}}筛选</el-button>
      </div>
    </div>
    <div class="search-row" v-if="searchId">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px" style="flex: 1" t>
        <el-form-item label="学校名称" prop="resourceName" >
          <el-input
            v-model="queryParams.name"
            placeholder="请输入学校名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="联系人" prop="contact" >
          <el-input
            v-model="queryParams.contact"
            placeholder="请输入联系人"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactMobile">
          <el-input
            v-model="queryParams.contactMobile"
            placeholder="请输入联系电话"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="gradeType">
          <el-select v-model="queryParams.status" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.classai_common_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学校类型" prop="subjectType">
          <el-select v-model="queryParams.schoolType" placeholder="请选择学校类型" >
            <el-option
              v-for="dict in dict.type.school_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item style="margin-left: 5%">-->
<!--          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
<!--          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
<!--        </el-form-item>-->
      </el-form>
      <div style="display: flex;height: 33px">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </div>
    </div>
<!--    <div style="margin-bottom: 24px">-->
<!--      <el-button type="primary" icon="el-icon-plus" @click="handleAdd">添加学校</el-button>-->
<!--    </div>-->
    <div class="schoolInfo">
      <div class="schoolInfo-li" v-for="item in schoolList">
        <div class="img-main">
          <img :src="item.logoPath" alt="">
          <div class="nameIn">
            <div class="nameIn-top">{{item.name}}</div>
            <div class="nameIn-bottom">{{item.area }}*{{item.schoolType ==0 ?'公立':item.schoolType ==1 ?'私立':''}}</div>
          </div>
        </div>
        <div class="schoolInfo-li-content">
          <div class="schoolInfo-li-content-row" style="margin-bottom: 12px">
            <div class="schoolInfo-li-content-row-left">
              <div class="schoolInfo-li-content-row-left-top">联系人</div>
              <div class="schoolInfo-li-content-row-left-bottom">{{ item.contact }}</div>
            </div>
            <div class="schoolInfo-li-content-row-right">
              <div class="schoolInfo-li-content-row-right-button" :class="item.status == 1 ? 'normal-button':item.status == 0 ? 'stop-button':''">
                {{ item.status == 1 ? '正常':item.status == 0 ? '停用': ''}}</div>
            </div>
          </div>
          <div class="schoolInfo-li-content-row">
            <div class="schoolInfo-li-content-row-left">
              <div class="schoolInfo-li-content-row-left-top">联系电话</div>
              <div class="schoolInfo-li-content-row-left-bottom">{{ item.contactMobile }}</div>
            </div>
            <div class="schoolInfo-li-content-row-right" style="display: flex;align-items: center">
              <img src="../../../../assets/images/edit.png" @click="handleUpdate(item)" style="margin-right: 16px" alt="">
              <!-- <img src="../../../../assets/images/delete.png" @click="handleDelete(item)" alt=""> -->
            </div>
          </div>
        </div>
      </div>
    </div>
    <div class="paginationCss">
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <div class="schoolEcharts">
      <div class="schoolEcharts-li" style="margin-right: 24px">
        <div class="schoolEcharts-li-title">学校类型统计</div>
        <div class="schoolEcharts-li-content">
          <pie-echarts />
        </div>
      </div>
      <div class="schoolEcharts-li">
        <div class="schoolEcharts-li-title">学校地区统计</div>
        <div class="schoolEcharts-li-content">
          <bar-echarts />
        </div>
      </div>
    </div>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['classai:school:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['classai:school:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['classai:school:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['classai:school:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

<!--    <el-table v-loading="loading" :data="schoolList" @selection-change="handleSelectionChange">-->
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="学校唯一标识" align="center" prop="id" />-->
<!--      <el-table-column label="学校名称" align="center" prop="name" />-->
<!--      <el-table-column label="省市" align="center" prop="city" />-->
<!--      <el-table-column label="市区" align="center" prop="area" />-->
<!--      <el-table-column label="学校地址" align="center" prop="address" />-->
<!--      <el-table-column label="邮政编码" align="center" prop="postalCode" />-->
<!--      <el-table-column label="学校类型" align="center" prop="schoolType">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.school_type" :value="scope.row.schoolType"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="成立日期" align="center" prop="establishedDate" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.establishedDate, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="学校简介" align="center" prop="description" />-->
<!--      <el-table-column label="校徽图片" align="center" prop="logoPath" width="100">-->
<!--        <template slot-scope="scope">-->
<!--          <image-preview :src="scope.row.logoPath" :width="50" :height="50"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="状态" align="center" prop="status">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.classai_common_status" :value="scope.row.status"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="联系人" align="center" prop="contact" />-->
<!--      <el-table-column label="联系电话" align="center" prop="contactMobile" />-->
<!--      <el-table-column label="邮箱" align="center" prop="email" />-->
<!--      <el-table-column label="附件" align="center" prop="attachment" />-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['classai:school:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['classai:school:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--    </el-table>-->



    <!-- 添加或修改学校信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" label-position="top">
        <div class="info-title">
          <img src="../../../../assets/images/basicInfo.png" alt="">
          <div class="info-title-right">基本信息</div>
        </div>
        <el-row style="display: flex;justify-content: space-between">
          <el-col :span="8">
            <el-form-item label="学校名称" prop="name" style="width: 90%">
              <el-input v-model="form.name" placeholder="请输入学校名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择状态" style="width: 90%">
                <el-option
                  v-for="dict in dict.type.classai_common_status"
                  :key="dict.value"
                  :label="dict.label"

                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="学校类型" prop="schoolType">
              <el-select v-model="form.schoolType" placeholder="请选择学校类型" style="width: 90%">
                <el-option
                  v-for="dict in dict.type.school_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display: flex;justify-content: flex-start">
          <el-col :span="8">
            <el-form-item label="建校时间" prop="establishedDate">
              <el-date-picker clearable
                              v-model="form.establishedDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              style="width: 90%"
                              placeholder="请选择建校时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display: flex;justify-content: flex-start">
          <el-col :span="8">
            <el-form-item label="学校背景" prop="logoPath">
              <image-upload v-model="form.logoPath" :limit="1"/>
            </el-form-item>
          </el-col>
        </el-row>
        <div class="info-title">
          <img src="../../../../assets/images/contactInfo.png" alt="">
          <div class="info-title-right">联系信息</div>
        </div>
        <el-row style="display: flex;justify-content: space-between">
          <el-col :span="8">
            <el-form-item label="联系人" prop="contact" style="width: 90%">
              <el-input v-model="form.contact" placeholder="请输入联系人" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系电话" prop="contactMobile" style="width: 90%">
              <el-input v-model="form.contactMobile" placeholder="请输入联系电话" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="电子邮箱" prop="email"  style="width: 90%">
              <el-input v-model="form.email" placeholder="请输入邮箱" />
            </el-form-item>
          </el-col>
        </el-row>
        <div class="info-title">
          <img src="../../../../assets/images/addressInfo.png" alt="">
          <div class="info-title-right">地址信息</div>
        </div>
        <el-row style="display: flex;justify-content: space-between">
          <el-col :span="8" style="display: flex">
            <el-form-item label="所在区域" prop="city" style="width: 44%;margin-right: 2%">
              <el-input v-model="form.city" placeholder="请输入省市" />
            </el-form-item>
            <el-form-item label=" -"  style="width: 44%">
              <el-input v-model="form.area" placeholder="请输入市区" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="详细地址" prop="address" style="width: 90%">
            <el-input v-model="form.address" placeholder="请输入学校地址" />
          </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="邮政编码" prop="postalCode" style="width: 90%">
              <el-input v-model="form.postalCode" placeholder="请输入邮政编码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display: flex;justify-content: flex-start">
          <el-col :span="24">
            <el-form-item label="学校简介" prop="description" style="width: 97%">
              <el-input v-model="form.description" :rows="5" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
        <div class="info-title">
          <img src="../../../../assets/images/attachment.png" alt="">
          <div class="info-title-right">附件材料</div>
        </div>
        <el-form-item label="附件" prop="attachment">
          <file-upload v-model="form.attachment" :fileSize ='10' :fileType="['pdf','jpg','png']" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSchool, getSchool, delSchool, addSchool, updateSchool } from "@/api/classai/school";
import pieEcharts from "@/views/components/pieEcharts1/index.vue";
import barEcharts from "@/views/components/barEcharts/index.vue";
import {countSchoolType, countSchoolCity} from "@/api/classai/countSchoolCity.ts";

export default {
  name: "School",
  dicts: ['classai_common_status', 'school_type'],
  components:{pieEcharts,barEcharts},
  data() {
    return {
      searchId:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学校信息表格数据
      schoolList: [],
      typeData: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        city: null,
        area: null,
        address: null,
        postalCode: null,
        schoolType: null,
        establishedDate: null,
        description: null,
        logoPath: null,
        status: null,
        contact: null,
        contactMobile: null,
        email: null,
        attachment: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "学校名称不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        schoolType: [
          { required: true, message: "学校类型不能为空", trigger: "change" }
        ],
        establishedDate: [
          { required: true, message: "建校时间不能为空", trigger: "change" }
        ],
        contact: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        contactMobile: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        email: [
          { required: true, message: "电子邮箱不能为空", trigger: "blur" }
        ],
        city: [
          { required: true, message: "所在区域不能为空", trigger: "blur" }
        ],
        area: [
          { required: true, message: "所在市区不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleSearch(){
      console.log('jinlai')
      this.searchId = !this.searchId
      console.log(this.searchId,'000000000000')
    },
    /** 查询学校信息列表 */
    getList() {
      this.loading = true;
      listSchool(this.queryParams).then(response => {
        this.schoolList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        city: null,
        area: null,
        address: null,
        postalCode: null,
        schoolType: null,
        establishedDate: null,
        description: null,
        logoPath: null,
        createTime: null,
        updateTime: null,
        status: null,
        contact: null,
        contactMobile: null,
        email: null,
        attachment: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.name = ''
      this.queryParams.status = ''
      this.queryParams.schoolType = ''
      this.queryParams.contact = ''
      this.queryParams.contactMobile = ''
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
    //   const routeData = this.$router.resolve({
    //     path: '/schoolDetails',
    //     query: { id: '',name:'添加新学校' } // 可选
    //   })
    //   window.open(routeData.href, '_blank') // _blank 表示新标签页
      this.$router.push({ path: '/school/schoolDetails', query: { id: '', name:'添加新学校' } })
      // this.reset();
      // this.open = true;
      // this.title = "添加学校信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({ path: '/school/schoolDetails', query: { id: row.id, name:'修改学校' } })

      // this.reset();
      // const id = row.id || this.ids
      // getSchool(id).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改学校信息";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateSchool(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addSchool(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      let arr = [row.id || this.ids]
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学校信息编号为"' + ids + '"的数据项？').then(function() {
        return delSchool(arr);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('classai/school/export', {
        ...this.queryParams
      }, `school_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
.school{
  width: 100%;
  height: 100%;
  padding: 24px;
  box-sizing: border-box;
  overflow-y: auto;
  background-color: #F3F4F6;
  .school-title{
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 24px;
    .school-title-left{
      font-size: 24px;
      font-weight: 600;
      line-height: 32px;
      color: #1F2937;
    }
    .school-title-right{
      display: flex;
    }
  }
  .search-row{
    padding: 24px 24px 0 24px;
    box-sizing: border-box;
    background-color: #ffffff;
    margin-bottom: 24px;
    border-radius: 8px;
    box-shadow: 0px 1px 2px -1px rgba(0, 0, 0, 0.1),0px 1px 3px 0px rgba(0, 0, 0, 0.1);
    display: flex;
    justify-content: space-between;
    //align-items: center;
  }
  .schoolInfo{
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    .schoolInfo-li{
      width: calc((100% - 72px) / 4);
      margin-right: 24px;
      border-radius: 10px;
      background-color: #ffffff;
      display: flex;
      flex-direction: column;
      margin-bottom: 24px;
      box-shadow: 0px 1px 2px -1px rgba(0, 0, 0, 0.1),0px 1px 3px 0px rgba(0, 0, 0, 0.1);
      .img-main{
        width: 100%;
        height: 192px;
        border-radius: 10px 10px 0 0;
        position: relative;
        img{
          width: 100%;
          height: 192px;
          border-radius: 10px 10px 0 0;
        }
        .nameIn{
          position: absolute;
          left: 16px;
          bottom: 16px;
          .nameIn-top{
            font-size: 20px;
            font-weight: bold;
            line-height: 28px;
            color: #FFFFFF;
            margin-bottom: 5px;
          }
          .nameIn-bottom{
            font-size: 14px;
            line-height: 20px;
            color: #E5E7EB;
          }
        }
      }

      .schoolInfo-li-content{
        flex: 1;
        padding: 16px;
        box-sizing: border-box;
        .schoolInfo-li-content-row{
          width: 100%;
          display: flex;
          justify-content: space-between;
          .schoolInfo-li-content-row-left{
            .schoolInfo-li-content-row-left-top{
              font-size: 14px;
              line-height: 20px;
              color: #6B7280;
            }
            .schoolInfo-li-content-row-left-bottom{
              font-size: 16px;
              font-weight: 500;
              line-height: 24px;
              color: #000000;
            }
          }
          .schoolInfo-li-content-row-right{
            img{
              height: 16px;
            }
            .schoolInfo-li-content-row-right-button{
              padding: 4px 8px;
              box-sizing: border-box;
              border-radius: 20px;
            }

          }
        }
      }
    }
    .schoolInfo-li:nth-child(4n+4){
      margin-right: 0;
    }
  }
  .paginationCss{
    padding: 0px 24px 16px 24px;
    box-sizing: border-box;
    background-color: #ffffff;
    border-top: 1px solid #E5E7EB;
    margin-bottom: 24px;
  }
  .schoolEcharts{
    width: 100%;
    display: flex;
    .schoolEcharts-li{
      flex: 1;
      border-radius: 8px;
      padding: 24px;
      box-sizing: border-box;
      box-shadow: 0px 1px 2px -1px rgba(0, 0, 0, 0.1),0px 1px 3px 0px rgba(0, 0, 0, 0.1);
      display: flex;
      flex-direction: column;
      background-color: #ffffff;
      .schoolEcharts-li-title{
        font-size: 18px;
        color: #111827;
        line-height: 28px;
      }
      .schoolEcharts-li-content{
        flex: 1;
      }
    }
  }
}
.normal-button{
  font-size: 12px;
  background: #DCFCE7;
  color: #166534;
}
.stop-button{
  font-size: 12px;
  background: #FEF9C3;
  color: #854D0E;
}
.info-title{
  width: 100%;
  display: flex;
  align-items: center;
  padding-bottom: 8px;
  box-sizing: border-box;
  border-bottom: 1px solid #E5E7EB;
  margin-bottom: 16px;
  img{
    height: 18px;
    margin-right: 10px;
  }
  .info-title-right{
    font-size: 18px;
    font-weight: 600;
    line-height: 28px;
    color: #374151;
  }
}
</style>
